package com.intelligent.ispc.utils;

import com.intelligent.ispc.core.dto.TestItemDto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by julia on 16/5/7.
 */
public class PageSubList {
    /**
     * @param list
     * @param currentPage
     * @param pagesize
     * @param totalcount
     * @return
     */
    public static List getPageList(List list, int currentPage, int pagesize, int totalcount) {
        int pagecount = 0;
        int m = totalcount%pagesize;
        if  (m > 0) {
            pagecount = totalcount/pagesize + 1;
        }
        else {
            pagecount=totalcount/pagesize;
        }
        currentPage = currentPage + 1;
        int fromIndex = 0;
        int toIndex = 0;
        if (list == null || list.size() == 0)
            return new ArrayList<>();
        if (currentPage <= pagecount && currentPage != 0) {
            fromIndex = (currentPage - 1) * pagesize;

            if (currentPage == pagecount) {
                toIndex = list.size();

            } else {
                toIndex = currentPage * pagesize;
            }

        }

        return list.subList(fromIndex, toIndex);
    }

    /**
     * @param list
     * @param currentPage
     * @param pagesize
     * @param totalcount
     * @param searchParams
     * @return
     */
    public static List getTestItemPageList(List list, int currentPage, int pagesize,
                                   int totalcount, Map<String, Object> searchParams) {
        int pagecount = 0;
        int m = totalcount%pagesize;
        if  (m > 0) {
            pagecount = totalcount/pagesize + 1;
        }
        else {
            pagecount=totalcount/pagesize;
        }
        currentPage = currentPage + 1;
        int fromIndex = 0;
        int toIndex = 0;
        if (list == null || list.size() == 0)
            return new ArrayList<>();
        if (currentPage <= pagecount && currentPage != 0) {
            fromIndex = (currentPage - 1) * pagesize;

            if (currentPage == pagecount) {
                toIndex = list.size();

            } else {
                toIndex = currentPage * pagesize;
            }

        }
        int first = 0;
        int last = 0;
        if (searchParams.containsKey("firstExcludeId")) {
            first = (int) searchParams.get("firstExcludeId");
        }
        if (searchParams.containsKey("lastExcludeId")) {
            last = (int) searchParams.get("lastExcludeId");
        }

        if (first < fromIndex || last > toIndex) {
            TestItemDto dd = (TestItemDto) list.get(fromIndex);
            dd.setHasExcludeId(true);
        }

        return list.subList(fromIndex, toIndex);
    }
}
